<?php
App::uses('AppController', 'Controller');
/**
 * Disciplines Controller
 *
 * @property Discipline $Discipline
 */
class DisciplinesController extends AppController {

	/**
	 * index method
	 *
	 * @return void
	 */
	public function index() {
		
		$this->set('disciplines', $this->paginate());

	}

	/**
	 * view method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function view($id = null) {
		$this->Discipline->id = $id;
		if (!$this->Discipline->exists()) {
			throw new NotFoundException(__('Invalid team'));
		}
		$this->set('discipline', $this->Discipline->read(null, $id));
	}

	/**
	 * add method
	 *
	 * @return void
	 */
	public function add() {
		$this->loadModel('Course');
		$this->loadModel('Team');
		$this->loadModel('CoursesDiscipline');
		$this->set('courses', $this->Course->find('list', array('fields' => array('Course.id', 'Course.name'))));
		$this->set('teamsL', $this->Team->find('list', array('fields' => array('Team.id', 'Team.name'))));
		if ($this->request->is('post')) {
			$this->Discipline->create();

			$this->request->data['Discipline']['_user'] = AuthComponent::user('id');
		
			if ($this->Discipline->save($this->request->data)) {

				$data = array();
				if(isset($this->request->data['courses']))
				{
					foreach ($this->request->data['courses'] as $key => $value) {
						$data[] = array('CoursesDiscipline' => array('discipline_id' => $this->Discipline->id, 'course_id' => $value));
					}
				}
				
				if(count($data))
					$this->CoursesDiscipline->saveMany($data);
				$this->Session->setFlash(__('The team has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash("A turma já existe");
			}
		}
	}

	public function beforeFilter(){
		$this->isAuthorized(array('admin', 'coordinator'));
	}

	public function addDiscipline(){
		$this->loadModel('Team');
		$disciplines = $this->Discipline->find('list', array('fields' => array('Discipline.id', 'Discipline.name')));
		$teams = $this->Team->find('list', array('fields' => array('Team.name', 'Team.id')));
		
		foreach ($disciplines as $key => $value) {
			$this->Discipline->read(null, $key);
			if($value)
				$data = array('Discipline' => array('_team' => $teams[$value]));
			$this->Discipline->save($data);
		}
	}

	/**
	 * edit method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function edit($id = null) {
		$this->Discipline->id = $id;
		$this->loadModel('Course');
		$this->loadModel('Team');
		$this->loadModel('CoursesDiscipline');
		$this->set('teamsL', $this->Team->find('list', array('fields' => array('Team.id', 'Team.name'))));
		$this->set('courses', $this->Course->find('list', array('fields' => array('Course.id', 'Course.name'))));
		if (!$this->Discipline->exists()) {
			throw new NotFoundException(__('Invalid team'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			$this->request->data['Discipline']['_user'] = AuthComponent::user('id');
			if ($this->Discipline->save($this->request->data)) {
				$data = array();
				if(isset($this->request->data['courses']))
					foreach ($this->request->data['courses'] as $key => $value) {
						$data[] = array('CoursesDiscipline' => array('discipline_id' => $this->Discipline->id, 'course_id' => $value));
					}
				if(count($data))
					$this->CoursesDiscipline->saveMany($data);
				$this->Session->setFlash(__('The team has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->request->data = $this->Discipline->read(null, $id);
				$this->Session->setFlash("A turma já existe");
			}
		} else {
			$this->request->data = $this->Discipline->read(null, $id);
		}
	}

	/**
	 * delete method
	 *
	 * @throws MethodNotAllowedException
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Discipline->id = $id;
		if (!$this->Discipline->exists()) {
			throw new NotFoundException(__('Invalid team'));
		}
		if ($this->Discipline->delete()) {
			$this->Session->setFlash(__('team deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('team was not deleted'));
		$this->redirect(array('action' => 'index'));
	}

}
